package org.scboy.coobi.db.style;

import org.apache.calcite.config.Lex;
import org.apache.calcite.sql.dialect.OracleSqlDialect;
import org.apache.calcite.sql.validate.SqlConformanceEnum;
import org.scboy.coobi.db.context.DbStyleContext;
import org.scboy.coobi.db.mapping.DataTypeMapping;
import org.scboy.coobi.db.mapping.oracle.OracleDataTypeMapping;
import org.scboy.coobi.entity.SqlConvertSchame;
import org.scboy.utils.entitys.coobi.DbColumn;

/**
 * Oracle 数据库操作类
 */
public class OracleStyle extends DbStyleContext {

	@Override
	public String getDb() {
		return "oracle";
	}
	
	@Override
	public String getJdbc() {
		return "jdbc:oracle:thin";
	}


	@Override
	public DataTypeMapping getDataTypeMapping() {
		return new OracleDataTypeMapping();
	}

	@Override
	public String getAddColumnCommentSql(String name, DbColumn column) {
		StringBuilder builder=new StringBuilder("COMMENT ON COLUMN ");
		builder.append(name);
		builder.append(".");
		builder.append(column.getName());
		builder.append(" IS '");
		builder.append(column.getComment());
		builder.append("'");
		return builder.toString();
	}

	@Override
	public String getAddTableCommentSql(String name, String comment) {
		StringBuilder builder=new StringBuilder("COMMENT ON TABLE ");
		builder.append(this.getFormatName(name));
		builder.append(" IS '");
		builder.append(comment);
		builder.append("'");
		return builder.toString();
	}

	@Override
	public String getExistsIndexSql(String tableName, String indexName) {
		String sql="SELECT INDEX_NAME,TABLE_NAME FROM USER_INDEXES WHERE INDEX_NAME = '"+indexName+"' AND TABLE_NAME = '"+tableName+"'";
		return this.sqlFormat(sql);
	}

	@Override
	public String getPageSql(String sql, int start, int limit) {
		int end=start+limit; 
		StringBuilder sb=new StringBuilder("SELECT * FROM (SELECT ROWNUM _R,T.* FROM ("); 
		sb.append(sql);
		sb.append(") T WHERE ROWNUM<="); 
		sb.append(end); 
		sb.append(" ) WHERE R >");
		sb.append(start);
		return sb.toString();
	}

	@Override
	public SqlConvertSchame getSchame() {
		return new SqlConvertSchame(Lex.ORACLE,SqlConformanceEnum.ORACLE_12, OracleSqlDialect.DEFAULT);
	}

	@Override
	public String getRString() {
		return "\"";
	}

	@Override
	public String getLString() {
		return "\"";
	}

}
